package com.education.wisdom.controller.app;

import com.education.common.pojo.SysUserOther;
import com.education.common.utils.LogUtil;
import com.education.wisdom.feign.UserFeign;
import com.education.wisdom.pojo.UserAccount;
import com.education.wisdom.service.ICurrencyRecordService;
import com.education.wisdom.service.IUserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.education.common.core.BaseController.writeJSON;

/**
 * 客户账户 信息操作处理
 *
 * @author ruoyi
 * @date 2018-11-20
 */
@Controller
@CrossOrigin
@Slf4j
@Api(tags = {"客户账户"})
@RequestMapping("/userAccount")
public class UserAccountController {

    @Autowired
    private IUserAccountService userAccountService;
    @Autowired
    private ICurrencyRecordService currencyRecordService;
    @Autowired
    private UserFeign userFeign;

    /**
     * 查询客户账户列表
     */
    @ApiOperation("根据用户ID获取用户账户信息")
    @ApiImplicitParam(name = "userId", value = "单个用户ID", dataType = "String")
    @GetMapping("/findByUserId")
    @ResponseBody
    public void list(String userId, String id, HttpServletResponse response) throws IOException {
        UserAccount user = userAccountService.selectUserAccountByUserId(userId);
        //添加用户昵称
//		SysAppLogin sysAppLogin = SysAppLoginService.selectSysAppLoginById(userId);
        SysUserOther sysUserOther = userFeign.selectOnePCF(userId);
        if (sysUserOther != null) {
            user.setUserAccount(sysUserOther.getSysRelname());
            user.setUserIco(sysUserOther.getSysIco());
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        BigDecimal income = currencyRecordService.incomeCurrencyRecord(map);
        BigDecimal expenditure = currencyRecordService.incomeCurrencyRecord(map);
        user.setIncome(income);
        user.setExpenditure(expenditure);
        writeJSON(user, response, "200", "成功");
    }


    /**
     * 苹果查询客户账户金额
     *
     * @param userId
     * @param id
     * @param response
     * @throws IOException
     */
    @ApiOperation("根据用户ID获取用户账户信息")
    @ApiImplicitParam(name = "userId", value = "单个用户ID", dataType = "String")
    @GetMapping("/findByUserIdios")
    @ResponseBody
    public void listIos(String userId, String id, HttpServletResponse response) throws IOException {
        SysUserOther sysUserOther = userFeign.selectOnePCF(userId);
        //用户达人币
        //sysUserOther.setCurrency(sysUserOther.getPrice());
        writeJSON(sysUserOther, response, "200", "成功");
    }


    /**
     * 查询用户账户剩余达人币
     */
    @ApiOperation("查询用户账户剩余达人币")
    @ApiImplicitParam(name = "userId", value = "单个用户ID", dataType = "String")
    @GetMapping("/findUserCurrency")
    @ResponseBody
    public void findUserCurrency(String userId, HttpServletResponse response) throws IOException {
        log.info(LogUtil.LogInfo("查询用户账户剩余达人币", new Date()));
        UserAccount user = userAccountService.selectUserCurrency(userId);
        writeJSON(user, response, "200", "成功");
    }

    /**
     * 查询达人币是否充足
     */
    @ApiOperation("查询达人币是否充足")
    @ApiImplicitParam(name = "userId", value = "单个用户ID", dataType = "String")
    @GetMapping("/findCurrency")
    @ResponseBody
    public void findCurrency(String userId, HttpServletResponse response, Integer currency) throws IOException {
        log.info(LogUtil.LogInfo("查询达人币是否充足", new Date()));
        UserAccount user = userAccountService.selectUserCurrency(userId);
        if (user.getCurrency() - currency >= 0) {
            writeJSON(user, response, "200", "达人币充足");
        } else {
            writeJSON(user, response, "201", "达人币不充足，请前往充值");
        }
    }


}
